/*
 * ShapefileDataConnection.java
 * 
 * Created by demory on Aug 8, 2009, 10:15:07 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.core.data;

import java.io.File;
import java.net.URL;
import java.util.Collection;
import org.fpdev.core.DataPackage;
import org.fpdev.core.FPEngine;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.geotools.data.FeatureStore;
import org.geotools.data.shapefile.ShapefileDataStore;

/**
 *
 * @author demory
 */
public class ShapefileDataConnection extends QueryableDataConnection {

  public ShapefileDataConnection(FPEngine engine, DataPackage dp) {
    super(engine);

    String home = engine_.getProperty("5pHome"), fs = File.separator;
    String dpName = dp.getName();

    try {
      File file = new File(home + "data" + fs + dpName + fs + "shp" + fs + "nodes.shp");
      if(!file.exists()) {
        System.out.println("Fatal error: nodes shapefile does not exist");
        System.exit(1);
      }
      URL shapeURL = file.toURI().toURL();
      ShapefileDataStore nodeStore = new ShapefileDataStore(shapeURL);
      nodeFeatStore_ = (FeatureStore) nodeStore.getFeatureSource(nodeStore.getTypeNames()[0]);

    } catch (Exception e) {
      System.out.println("Error reading nodes shapefile:");
      e.printStackTrace();
    }

    try {
      File file = new File(home + "data" + fs + dpName + fs + "shp" + fs + "links.shp");
      if(!file.exists()) {
        System.out.println("Fatal error: links shapefile does not exist");
        System.exit(1);
      }
      URL shapeURL = file.toURI().toURL();
      ShapefileDataStore linkStore = new ShapefileDataStore(shapeURL);
      linkFeatStore_ = (FeatureStore) linkStore.getFeatureSource(linkStore.getTypeNames()[0]);

    } catch (Exception e) {
      System.out.println("Error reading links shapefile:");
      e.printStackTrace();
    }

  }

  public String getGeomFieldName() {
    return "the_geom";
  }

  public void close() {
    System.out.println("closing shapefile connection");
  }

  // empty write methods:

  public void createNode(BNode node) { }

  public void deleteNode(BNode node) { }

  public void createLink(BLink link) { }

  public void deleteLink(BLink link) { }

  public void modifyNode(BNode node, NodeAttr[] attrs) { }

  public void modifyLink(BLink link, LinkAttr[] attrs) { }

  public void modifyNodes(Collection<BNode> nodes, NodeAttr[] attrs) { }

  public void modifyLinks(Collection<BLink> links, LinkAttr[] attrs) { }


}
